#include "TrajSimul.h"

#include "EvolveAlgo.h"
#include "TrajSave.h"
#include "LimTorqueCheck.h"

#include <iostream>

TrajSimul::TrajSimul()
{
 
}

TrajSimul::~TrajSimul()
{

}

MultiLayer TrajSimul::solve(Clock clock,Output output,Pulse *field,Pulse *current,MultiLayer multiLayer,NewParameters params)
{
  // int layerNumber=multiLayer.getSolveLayer();
  std::cout<<"H="<<field->getLevel(clock.getTimeStamp())<<std::endl;
  std::cout<<"I="<<current->getLevel(clock.getTimeStamp())<<std::endl;
  Check limtorque;

  TrajSave().init(output,multiLayer);   
  for(clock.setTimeStamp(0);clock.getTimeStamp()<clock.getTimeWindow();clock.setTimeStamp(clock.getTimeStamp()+clock.getTimeStep()))
    {
      if(clock.getTimeStep()>clock.getMaxTimeStep()) 
	{	    
	  clock.setTimeStep(clock.getMaxTimeStep());
	}		  
      MultiLayer result=EvolveAlgo().solve(clock,field,current,multiLayer,params);
      multiLayer=result;
      //std::cout<<"trajsimul:"<<multiLayer[0].getMag().getZ()<<std::endl;
      TrajSave().save(clock,output,field,current,multiLayer);
      limtorque=LimTorqueCheck(clock,field,current,multiLayer,params);
      if (limtorque.getCheck()==true)
	{
	  //std::cout<<"limtorque!\n"<<std::endl;	  
	  return multiLayer;
	}
    }
  return multiLayer;
  
}
